<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Models\Coupon;
use Illuminate\Http\Request;

class CouponController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return $this->success(Coupon::paginate(10));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
//        $start_time = strtotime($request->input('start_time'));
//        $end_time = strtotime($request->input('end_time'));
        $request->merge(['start_time'=>strtotime($request->input('start_time'))]);
        $request->merge(['end_time'=>strtotime($request->input('end_time'))]);
//        $money = $request->input('money');
//        $name = $request->input('name');
//        $type = $request->input('type');
//        if (is_numeric($start_time)  ) {
//            Coupon::create([
//                'start_time' => $start_time,
//                'end_time' => $end_time,
//                'money' => $money,
//                'name' => $name,
//                'type' => $type
//            ]);
//            return $this->success();
//        }else{
//
//        }
//        dd($request->all());
        Coupon::create($request->all());
        return $this->success();

//        $this->error();
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if (Coupon::where('id', $id)->update($request->all())) return $this->success();
        $this->error();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if (Coupon::destroy($id)) return $this->success();
        $this->error();
    }
}
